import * as PIXI from "pixi.js";
import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'
// import {PixiApp} from "@/pixi/App";
import {bgContainer} from "@/pixi/bgContainer";
import {isString} from '@/utils/index'

export class CreateBg{
    constructor(_params) {
        let {url,bgWidth,bgHeight} = _params
        // 创建一个纹理 底图
        let texture = null;
        if(isString(url) && url){
            texture = PIXI.Texture.from(url);
        }
        // 创建一个精灵
        let sprite = new PIXI.Sprite(texture);
        sprite.name = 'bg'

        // 改变小精灵的宽高 拉伸模式 全部铺满
        let store = usePixiStore()
        store.changeCurrentWidth()
        store.changeCurrentHeight()
        let {pixiMaxWidth,pixiMaxHeight,currentWidth,currentHeight } = storeToRefs(store)

        // sprite.width = bgWidth?bgWidth:currentWidth.value;
        // sprite.height = bgHeight?bgHeight:currentHeight.value;

        sprite.width = bgWidth?bgWidth/pixiMaxWidth.value*currentWidth.value:currentWidth.value;
        sprite.height = bgHeight?bgHeight/pixiMaxHeight.value*currentHeight.value:currentHeight.value;


        // let pixiApp = PixiApp.init()
        // sprite.width = pixiApp.app.screen.width;
        // sprite.height = pixiApp.app.screen.height;
        // 设置精灵的锚点
        sprite.anchor.set(0.5, 0.5);
        // sprite.interactive = true;
        sprite.eventMode = 'static';
        bgContainer.addChild(sprite);
    }

    static create(_params){
        return new CreateBg(_params)
        // let store = pixiStore()
        // let { _containerBg} = store
        // if(_containerBg){
        //     return toRaw(_containerBg)
        // }else{
        //     store.changeContainerBg(new CreateBg(_params))
        //     return toRaw(store._containerBg)
        // }
    }


}
